//
//  PopoverAnimator.swift
//  HJWeibo
//
//  Created by huangjiong on 16/2/2.
//  Copyright © 2016年 huangjiong. All rights reserved.
 //本类,专门用于首页弹出菜单时的转场

import UIKit

let kHomeMenuWillShow = "HomeMenuWillShow"
let kHomeMenuWillDismiss = "HomeMenuWillDismiss"

class PopoverAnimator: NSObject ,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning
{
    
    var isPresent: Bool = false
    var presentFrame: CGRect = CGRectZero
    
    // 一.实现代理方法, 告诉系统谁来负责转场
    // UIPresentationController iOS8推出的专门用于负责转场动画的
    @available(iOS 8.0, *)
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        
        if #available(iOS 8.0, *) {
            
            let popVC = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
            popVC.presentFrame = presentFrame
            return popVC
        } else {
            return nil
        }
    }
    
    //二. 再实现下面这两个代理方法,告诉系统由谁来负责modal动画的展现
    //注意:只要我们告诉谁来负责动画,那么所有的动画内容系统就不会再做了,而要由我们自己来实现
    //1.告诉系统动画由谁来展现,一般就由当前控制器
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        //发送通知,告诉首页视图弹出来了
        NSNotificationCenter.defaultCenter().postNotificationName(kHomeMenuWillShow, object: self)
        
        //弹出动画时,记录状态
        isPresent = true
        return self
    }
    //告诉系统动画由谁来负责消失
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        //发送通知,告诉首页视图消失了
        NSNotificationCenter.defaultCenter().postNotificationName(kHomeMenuWillDismiss, object: self)
        //动画消失时,记录状态
        isPresent = false
        //因为动画就是在当前控制器之上,所以一般就由当前控制器来负责
        //负责实现动画也是通过代理方法UIViewControllerAnimatedTransitioning
        return self
    }
    
    //上面是转场代理方法
    //下面来实现动画的代理方法
    
    /**
    一.动画时长
    
    - parameter transitionContext: 上下文
    
    - returns: 时长
    */
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 0.5
    }
    
    /**
     二.告诉系统如何动画
     
     - parameter transitionContext: 上下文,所有参数都可从中获取
     */
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        
        if isPresent
        {
            if #available(iOS 8.0, *)
            {
                //一.展开
                //1.取到要展现的视图,也可以取到要展现的控制器
               
                let toView =  transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!.view
                //2.将要展现的视图添加到容器中
                transitionContext.containerView()?.addSubview(toView)
                //3.设置动画内容
                toView.transform = CGAffineTransformMakeScale(1.0, 0)
                toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
                //4执行动画
                UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                    
                    //4.1清空transform
                    toView.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        //4.2动画执行完毕,一定告诉系统,否则可能会出错
                        transitionContext.completeTransition(true)
                })
            } else {}
            
        }else
        {
            if #available(iOS 8.0, *) {
                //二.消失
                //1.先取到要消失的view
                let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
                //2.消失
                UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                    
                    //压扁
                    fromView?.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                    }, completion: { (_) -> Void in
                        //告诉系统动画执行完毕
                        transitionContext.completeTransition(true)
                })
            } else {}
            
        }
    }
    
}
